package com.anolesoft.yr.store.stk.ctrl;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.core.web.page.TableDataInfo;
import com.anolesoft.system.fac.entity.Employee;
import com.anolesoft.system.fac.service.IEmployeeService;
import com.anolesoft.yr.store.stk.entity.StkOutinType;
import com.anolesoft.yr.store.stk.entity.StkOutinType;
import com.anolesoft.yr.store.stk.entity.StkOutinTypeExample;
import com.anolesoft.yr.store.stk.service.StkOutinTypeService;
import com.anolesoft.yr.store.stk.service.StkTypeService;
import com.anolesoft.yr.store.stk.util.StringUtil;
import com.anolesoft.yr.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Archie
 */
@AllArgsConstructor
@RestController()
@RequestMapping("/stk/stk_outin_type")
public class StkOutInTypeController {
    private StkOutinTypeService stkOutinTypeService;
    private IEmployeeService iEmployeeService;
    @Transactional
    @RequestMapping(value = "/get", produces = {"application/json;charset=UTF-8"})
    public AjaxResult get(StkOutinType stkOutinType){
        Page<StkOutinType> page = new Page<>(stkOutinType.getPage(), stkOutinType.getRows());
        String stkName = StrUtil.nullToEmpty(stkOutinType.getOutinType());
        LambdaQueryWrapper<StkOutinType> like = new QueryWrapper<StkOutinType>()
                .lambda()
                .like(StkOutinType::getOutinType, stkName)
                .orderByDesc(StkOutinType::getcTime);

        Page<StkOutinType> result = stkOutinTypeService.page(page, like);
        LambdaQueryWrapper<Employee> emplike = new QueryWrapper<Employee>()
                .lambda();
        List<Employee> employees = iEmployeeService.getBaseMapper().selectList(emplike);
        Map<String, String> collect = employees.stream().collect(Collectors.toMap(Employee::getObjuid, Employee::getEmpName));


        List<StkOutinType> records = result.getRecords();

        for (int i = 0; i < records.size(); i++) {
            if(records.get(i).getcUserUid()!=null){
                records.get(i).setcUserUid(collect.get(records.get(i).getcUserUid()));
            }
        }
        return AjaxResult.success(new TableDataInfo(        result.getTotal(),   records    ));
    }
    @RequestMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    public AjaxResult del(StkOutinType stkOutinType){

        stkOutinTypeService.removeById(stkOutinType.getObjuid());
        return AjaxResult.success("删除成功");
    }

    @Transactional
    @RequestMapping(value = "/delBatch", produces = {"application/json;charset=UTF-8"})
    public AjaxResult delBatch(@RequestBody List<StkOutinType> stkTypes){
        stkOutinTypeService.removeByIds(stkTypes.stream().map(StkOutinType::getObjuid).collect(Collectors.toList()));
        return AjaxResult.success("删除成功");
    }
    @Transactional
    @RequestMapping(value = "/edit", produces = {"application/json;charset=UTF-8"})
    public AjaxResult edit(StkOutinType stkOutinType){
        String empUid = SecurityUtils.getLoginUser().getEmpUid();
        stkOutinType.setmUserUid(empUid);
        stkOutinType.setmTime(new Date());
        stkOutinTypeService.updateById(stkOutinType);
        return AjaxResult.success("修改成功");
    }
    @Transactional
    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public AjaxResult add(StkOutinType stkOutinType){
        StkOutinTypeExample stkTypeExample = new StkOutinTypeExample();
        stkTypeExample.createCriteria().andOutinTypeEqualTo(stkOutinType.getOutinType());
        List<StkOutinType> stkTypes = stkOutinTypeService.selectByExample(stkTypeExample);
        if (stkTypes.isEmpty()){
            String empUid = SecurityUtils.getLoginUser().getEmpUid();
            stkOutinType.setcUserUid(empUid);
            stkOutinType.setcTime(new Date());
            stkOutinType.setObjuid(StringUtil.generateRundom32String());
            stkOutinTypeService.save(stkOutinType);
            return AjaxResult.success("保存成功");
        }else{
            return AjaxResult.success("保存失败，类型名不可以重复");
        }
    }

    @RequestMapping(value = "/unique",  produces = {"application/json;charset=UTF-8"})
    public boolean unique(String stkOutinType,String objuid){
        objuid = StrUtil.nullToEmpty(objuid);
        StkOutinTypeExample stkTypeExample = new StkOutinTypeExample();
        stkTypeExample.createCriteria().andOutinTypeEqualTo(stkOutinType).andObjuidNotEqualTo(objuid);
        List<StkOutinType> stkTypes = stkOutinTypeService.selectByExample(stkTypeExample);
        if (!stkTypes.isEmpty()) {
            return false;
        }else {
            return true;
        }
    }
    @Transactional
    @RequestMapping(value = "/getById" )
    public AjaxResult getById(@RequestBody StkOutinType stkOutinType){
        StkOutinType result = stkOutinTypeService.getById(stkOutinType.getObjuid());
        return AjaxResult.success(result);
    }
}
